package com.shell.customer.accreditation.rx;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.shell.bee.base.exception.UnCheckMsgException;
import com.shell.bee.base.utils.Encryptor;
import com.shell.bee.base.utils.JsonUtil;
import com.shell.bee.base.utils.RandomUtil;
import com.shell.bee.base.utils.SpringContextUtil;
import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.customer.accreditation.AccreditEvent;
import com.shell.customer.accreditation.vo.AccreditationVo;
import com.shell.customer.accreditation.vo.UserVo;
import com.shell.customer.base.dao.CusTagDao;
import com.shell.customer.po.CusRec;
import com.shell.dto.UserAndOrganDto;
import com.shell.feign.PubDictionaryFeign;
import com.shell.feign.PubUserFeign;
import com.shell.feign.UserOrganFeign;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.shell.governor.modules.po.PubDictionary;
import com.shell.governor.organization.vo.PubOrganVo;
import com.shell.governor.user.vo.PubUserVo;

@Component
public class UpdateUserAndOrganSubscriber extends OnErrorNotImplementedSubscriber<AccreditEvent> {
	@Autowired
	private PubDictionaryFeign pubDictionaryFeign;
	@Autowired
	private PubUserFeign pubUserFeign;
	@Autowired
	private UserOrganFeign userOrganFeign;
	@Autowired
	private CusTagDao cusTagDao;

	public void onApplicationEvent(AccreditEvent event) {
		AccreditationVo accreditationVo = event.getAccreditationVo();
		CusRec cus = accreditationVo.getCustomerC();

		PubOrganVo organVo = new PubOrganVo();
		organVo.setOrganId(cus.getCusId());
		organVo.setOrganCode(cus.getCusCode());
		organVo.setOrganName(cus.getCusName());
		organVo.setShortName(cus.getCusName());
		organVo.setOrganState("E");
		organVo.setOrganType("ENT");
		organVo.setSourceCode("EXTERNAL");
		PubDictionary dict = this.pubDictionaryFeign.getDictionaryByCode(cus.getCusType());
		if (StringUtil.notTrimEmpty(dict.getExtension1())) {
			organVo.setParentCode(dict.getExtension1());
		} else {
			organVo.setParentCode("OG001");
		}
		String adminId = cus.getContactorId();

		List<PubUserVo> users = this.pubUserFeign.selectAllUserForEnt(cus.getCusId());
		List<PubUserVo> userVos = new ArrayList();
		for (PubUserVo userVo : users) {
			if (userVo.getUserId().equals(adminId)) {
				UserVo admin = new UserVo();
				admin.setUserName(userVo.getUserName());
				accreditationVo.setAdministrator(admin);
			}
			userVo.setEntId(organVo.getOrganId());
			userVo.setOrganId(organVo.getOrganId());
			userVo.setUserOrganValid("1");
			userVo.setGrpId(cus.getGrpId());
			if (userVo.getUserState().equals("I")) {
				userVo.setUserState("E");
				String password = "";
				if (!SystemUtil.SystemMode.prod.equals(SystemUtil.getSystemMode())) {
					password = SpringContextUtil.getProperty("user.default.password");
				} else {
					password = String.valueOf(RandomUtil.getRandomInt(8));
				}
				userVo.setPassword(Encryptor.encryptWithMD5(password));
				userVo.setUnencryptedPassword(password);
				userVo.setNeedSync(event.isNeedSync());
			}
			userVos.add(userVo);
		}
		String bizIds = this.cusTagDao.selectCusTagBizIds(cus.getCusId());
		organVo.setBizIds(bizIds);

		UserAndOrganDto dto = new UserAndOrganDto();
		dto.setUsers(userVos);
		dto.setOrgan(organVo);
		event.setUserVos(userVos);
		try {
			this.userOrganFeign.saveOrUpdateUserAndOrgan(JsonUtil.toJson(dto));
		} catch (Exception e) {
			e.printStackTrace();
			throw new UnCheckMsgException("������������������������");
		}
	}
}
